package com.kai.qiang.ui

import android.content.Intent
import android.view.KeyEvent
import androidx.fragment.app.Fragment
import com.hwangjr.rxbus.RxBus
import com.hwangjr.rxbus.annotation.Subscribe
import com.hwangjr.rxbus.thread.EventThread
import com.kai.base.base.activity.BaseVBActivity
import com.kai.base.base.adapter.BaseFragmentPageAdapter
import com.kai.base.utils.LogUtils
import com.kai.base.widget.btm_nav_view.BtmNavItemView
import com.kai.base.widget.btm_nav_view.BtmNavViewListener
import com.kai.base.widget.btm_nav_view.LottieNavItemView
import com.kai.lib_app.core.FRAGMENT_ID_CARTOON
import com.kai.lib_app.core.FRAGMENT_ID_RANKING
import com.kai.lib_app.core.RouterHub
import com.kai.lib_app.data.event.EndHomeAnimEvent
import com.kai.lib_app.data.event.UpdateMainEvent
import com.kai.lib_app.helper.RouteHelper
import com.kai.qiang.R
import com.kai.qiang.databinding.ActivityMainBinding
import java.util.*
import kotlin.collections.ArrayList

class MainActivity : BaseVBActivity<ActivityMainBinding>(), BtmNavViewListener {

    override fun getViewBinding(): ActivityMainBinding = ActivityMainBinding.inflate(layoutInflater)

    private val mFragments = ArrayList<Fragment>()

    private val items = TreeMap<Int, BtmNavItemView>()

    override fun isRegisterRxBus() = true

    override fun initViews() {
        loadFragments()
        val pageAdapter = BaseFragmentPageAdapter(supportFragmentManager, mFragments)
        mBaseActViewBind.apply {
            mViewPager.adapter = pageAdapter
            bottomBar.create(items, 0)
            bottomBar.setBtmNavViewListener(this@MainActivity)
        }
    }


    private fun loadFragments() {
        loadCartoon()
        loadRanking()
    }

    private fun loadCartoon() {
        val cartoon = RouteHelper.obtainARouterFragment(RouterHub.ROUTER_CARTOON_FRAGMENT)
        if (cartoon != null) {
            mFragments.add(cartoon)
            items[FRAGMENT_ID_CARTOON] = LottieNavItemView(
                this, FRAGMENT_ID_CARTOON, getString(R.string.app_menu_cartoon)
                , animRes = R.drawable.anim_menu_loading1
                , unSelectedIconId = R.mipmap.ic_qiushi_normal
                , selectedIconId = R.mipmap.ic_qiushi_select
                , refreshAnimationIconId = R.mipmap.ic_qiushi_refresh
                , enableSameClickAnimation = true
            )
        }
    }

    private fun loadRanking() {
        val ranking = RouteHelper.obtainARouterFragment(RouterHub.ROUTER_RANKING_FRAGMENT)
        if (ranking != null) {
            mFragments.add(ranking)
            items[FRAGMENT_ID_RANKING] = LottieNavItemView(
                this, FRAGMENT_ID_RANKING, getString(R.string.app_menu_ranking)
                , animRes = R.drawable.anim_menu_loading1
                , unSelectedIconId = R.mipmap.ic_qiushi_normal
                , selectedIconId = R.mipmap.ic_qiushi_select
                , refreshAnimationIconId = R.mipmap.ic_qiushi_refresh
            )
        }
    }

    /**   已经选中时的点击事件  */
    override fun onBtmNavViewSameClick(id: Int) {
        RxBus.get().post(UpdateMainEvent(id))
    }

    /**   点击事件 */
    override fun onBtmNavViewClick(id: Int, oldId: Int) {
        mBaseActViewBind.mViewPager?.currentItem = id
    }

    @Subscribe(thread = EventThread.MAIN_THREAD)
    fun onMessageEvent(messageEvent: EndHomeAnimEvent) {
        mBaseActViewBind.bottomBar?.endSameClickAnimation(messageEvent.index)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            val home = Intent(Intent.ACTION_MAIN)
            home.addCategory(Intent.CATEGORY_HOME)
            startActivity(home)
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}
